using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Barbar.MapReport.Common;
using Barbar.MapReport.Common.Drawing;
using Barbar.MapReport.DataSource.Facade;
using Barbar.MapReport.HtmlRender;
using Barbar.MapReport.Common.Drawing.Commands;
using System.Collections.ObjectModel;
using System.Drawing;
using Barbar.MapReport.Configuration;

public partial class ResponseRatioReport : MapReportDemoPage {
  protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
      ddlResponseType.DataValueField = "Index";
      ddlResponseType.DataTextField = "Name";
      ddlResponseType.DataSource = ResponseType.Responses;
      ddlResponseType.DataBind();

      ddlResponseType.SelectedValue = Convert.ToString(ResponseType.NotInterested.Index);
    }

    ResponseType selectedResponse = ResponseType.GetResponseType(Convert.ToInt32(ddlResponseType.SelectedValue));

    SortedList<string, ContactRegionGroup> groups = ContactRegionGroup.CreateContactRegionGroups();
    List<BusinessRegionList> valueRegionList = new List<BusinessRegionList>();
    foreach (ContactRegionGroup group in groups.Values) {
      ResponseGroup responses = new ResponseGroup();
      foreach (Contact contact in group) {
        responses.Add(contact.ResponseType);
      }
      if (!responses.HasResponse(selectedResponse))
        continue;

      BusinessRegionList valueRegion = new BusinessRegionList();
      valueRegion.RegionIds.Add(group.Region.Id);
      valueRegion.Value = responses.GetPercentValue(selectedResponse);

      StringBuilder colorBuilder = new StringBuilder();
      StringBuilder valueBuilder = new StringBuilder();
      StringBuilder nameBuilder = new StringBuilder();

      bool isFirst = true;
      foreach (KeyValuePair<ResponseType, int> pair in responses) {
        int percentValue = responses.GetPercentValue(pair.Key);
        if (!isFirst) {
          colorBuilder.Append(",");
          valueBuilder.Append(",");
          nameBuilder.Append("|");
        } else
          isFirst = false;
        colorBuilder.Append(pair.Key.TextColor);
        valueBuilder.AppendFormat("{0}.0", pair.Value);
        nameBuilder.AppendFormat("{0} - {1}%", pair.Key.Name, percentValue);
      }

      valueRegion.ToolTip = String.Format(@"
<span class=""field"">Response ratio for {{Name}}:</span><br/>
<img src=""http://chart.apis.google.com/chart?cht=p3&chd=t:{0}&chs=400x100&chl={1}&chco={2}"" style=""width:400px; height:100px"" /><br/>
<a href=""ResponseDistributionReport.aspx?regionId={3}"" target=""_self"" class=""field"">Show Details</a>
",
  valueBuilder, nameBuilder, colorBuilder, valueRegion.RegionIds[0]);
      valueRegionList.Add(valueRegion);
    }

    Collection<BusinessGeoPoint> centerPoints = new Collection<BusinessGeoPoint>();

    IDataSourceService service = new DataSourceService();
    foreach (BusinessRegionList geoValueRegion in valueRegionList) {
      RegionRequestById request = new RegionRequestById();
      request.RegionId = geoValueRegion.RegionIds[0];
      request.IncludeGeoBound = true;

      SingleRegionResponse response = (SingleRegionResponse)service.GetRegions(request);
      GeoBound bound = response.GeoBound;
      centerPoints.Add(new BusinessGeoPoint(bound.Center.Latitude, bound.Center.Longitude, geoValueRegion.Value, String.Format("{0} %", (int)geoValueRegion.Value), null, null));
    }

    RenderingService renderService = new RenderingService();

    AddValueIndicatorsCommand command = new AddValueIndicatorsCommand();
    foreach (BusinessGeoPoint point in centerPoints)
      command.Points.Add(point);
    command.MaxSize = 8;
    command.MinSize = 8;
    command.Color = new SerializedColor(Color.Green);
    renderService.Execute(command);

    AddRegionsWithBusinessProcessingCommand regionsCommand = new AddRegionsWithBusinessProcessingCommand();
    foreach (BusinessRegionList regionList in valueRegionList)
      regionsCommand.Regions.Add(regionList);
    regionsCommand.RegionOptions = new RegionOptions();
    regionsCommand.RegionOptions.CategoriesCount = 3;
    regionsCommand.RegionOptions.Opacity = 0.3f;
    regionsCommand.RegionOptions.PolygonBehavior = new PolygonBehaviorBubble("{Content}");
    regionsCommand.LegendDescription = new LegendDescription(Color.Red, "Responses:", "%");
    renderService.Execute(regionsCommand);

    RenderSettings settings = new RenderSettings(new SerializedUnit(800), new SerializedUnit(600), 51, 11, 6, MapConfigurationManager.MapSettings.GoogleKeyProvider.DefaultGoogleKey, 2057);
    settings.EnableGoogleBar = true;

    MapHtmlFragment fragment = renderService.Render(settings);
    litMapDiv.Text = fragment.MapElement + fragment.LegendElements[0];
  }
}
